package com.koron.css2.newProjectCenter.print;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swan.bean.MessageBean;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 施工回执单打印
 */
public class PjConstructionReceiptPrint implements ServerInterface {

    // 每次最多打印多少条
    private static final int BATCH_COUNT = 1000;

    private static Logger logger = LoggerFactory.getLogger(PjConstructionReceiptPrint.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            List<String> idList = JsonUtils.objectToList(req.getData(), String.class);
            if (idList.size() == 0){
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请勾选需要打印的数据", String.class);
            }
            // 打印条数限制
            if (idList.size() > BATCH_COUNT){
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "每次最多能打印" + BATCH_COUNT + "条", String.class);
            }
            ArrayList<HashMap<String, String>> result = getPrintDate(factory, idList);
            String key = PrintInterfaceImpl.setPrintData(result);
            List<String> keys = new ArrayList<String>();
            keys.add(key);
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "施工回执单打印成功", List.class);
            messageBean.setData(keys);
            return messageBean;
        }catch (Exception exception){
            logger.error("施工回执单打印失败：" + exception.getMessage());
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "施工回执单打印失败", List.class);
    }

    /**
     * 获取打印数据
     * @param factory
     * @param idList 水表ID集合
     * @return
     */
    private ArrayList<HashMap<String, String>> getPrintDate(SessionFactory factory, List<String> idList) {
        ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
        MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
        UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
        PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);

        // 通过水表ID查询出水表信息
        List<PjMeter> pjMeterList = pjMeterMapper.getListByIdList(idList);
        if (pjMeterList.size() == 0){
            return new ArrayList<>();
        }
        // 将临时表用户信息ID 转成集合
        List<String> userInfoIdList = pjMeterList.stream().map(t -> t.getUserInfoTempId()).collect(Collectors.toList());
        // 获取用户信息
        List<UserBean> userList = userMapper.selectListByIds(userInfoIdList, UserBean.USERTEMP);
        // 获取水表ID
        List<String> msInfoIdList = pjMeterList.stream().filter(t -> StringUtils.isNotBlank(t.getMsInfoId())).map(t -> t.getMsInfoId()).collect(Collectors.toList());
        // 水表信息集合
        List<MsInfoDto> msInfoList = new ArrayList<>();
        if (msInfoIdList.size() > 0){
            // 查询出水表信息
            msInfoList = msInfoMapper.getMsInfoByIds(msInfoIdList);
        }
        // 将用户集合转成Map
        Map<String, MsInfoDto> msInfoMap = msInfoList.stream().collect(Collectors.toMap(MsInfoDto::getId, Function.identity()));
        // 将用户集合转成Map
        Map<String, UserBean> userMap = userList.stream().collect(Collectors.toMap(UserBean::getId, Function.identity()));
        for (int i = 0; i < pjMeterList.size(); i++){
            PjMeter pjMeter = pjMeterList.get(i);
            HashMap<String, String> hashMap = new HashMap<>();
            UserBean user = userMap.get(pjMeter.getUserInfoTempId());
            MsInfoDto msInfo = msInfoMap.get(pjMeter.getMsInfoId());
            // 工程安装地址
            hashMap.put("setupMeterAddr", Objects.toString(user.getSetupMeterAddr(), ""));
            // 客户名称
            hashMap.put("ctmName",Objects.toString(user.getCtmName(), ""));
            // 手机号
            hashMap.put("mobile", Objects.toString(user.getMobile(), ""));
            // 安装日期
            hashMap.put("installDate", Objects.toString(user.getSetupMeterDate(), ""));
            // 表身码
            if (msInfo != null){
                hashMap.put("factoryNo", Objects.toString(msInfo.getFactoryNo(), ""));
            }else {
                hashMap.put("factoryNo", "");
            }
            result.add(hashMap);
        }
        return result;
    }
}
